package com.aube.interact.model;

import android.content.Context;

import com.aube.interact.controller.DataManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class VoteInfo implements Serializable
{
    /**
     * 注释内容
     */
    private int totalNum;
    
    private String sameNum;

    public String rid;
    
    private List<VoteItemInfo> contents;
    
    private String title;
    
    private VoteType type;
    
    private boolean showMore = true;//是否显示更多
    
    private String originVoteId = "";
    
    public enum VoteType
    {
        SUPPORT, COMMON
    }
    
    public int getTotalNum()
    {
        return totalNum;
    }

    public void setTotalNum(int totalNum)
    {
        this.totalNum = totalNum;
    }
    
    public String getSameNum()
    {
        return sameNum;
    }
    
    public void setSameNum(String sameNum)
    {
        this.sameNum = sameNum;
    }
    
    public boolean isVoted(Context context)
    {
        return getVotedIndex(context) >=0;
    }
    
    public boolean isShowMore()
    {
        return showMore;
    }
    
    public void setShowMore(boolean show)
    {
        this.showMore = show;
    }
    
    public int getVotedIndex(Context context){
//    	if (contents!=null) {
//			for (int i = 0; i < contents.size(); i++) {
//				if ("1".equals(contents.get(i).getStatus())) {
//					return i;
//				}
//			}
//		}
//    	return -1;
//        return DataStateController.getIndex(rid);
        return DataManager.getInstance(context).getIndex(rid);
    }
    
    public String getVotedId(){
    	if (contents!=null) {
			for (int i = 0; i < contents.size(); i++) {
				if ("1".equals(contents.get(i).getStatus())) {
					return contents.get(i).getId();
				}
			}
		}
    	return "0";
    }
    

    public VoteType getType()
    {
        return type;
    }
    
    public void setType(VoteType type)
    {
        this.type = type;
    }
    

    public List<VoteItemInfo> getContents()
    {
        return contents;
    }
    
    public void setContents(List<VoteItemInfo> contents)
    {
        this.contents = contents;
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public void setTitle(String title)
    {
        this.title = title;
    }
//    public void onVote(int index){
//    	if (!isVoted()) {
//    		totalNum ++;
//		}
//    	if (contents != null) {
//			for(VoteItemInfo info : contents){
//				if ("1".equals(info.getStatus())&&info.getNumber() > 0) {
//					info.setNumber(info.getNumber()-1);
//					info.setStatus("0");
//				}
//			}
//		}
//    	if (contents != null && contents.size() > index) {
//    		VoteItemInfo info = contents.get(index);
//			info.setStatus("1");
//			if (info.getNumber() < getTotalNum()) {
//				info.setNumber(info.getNumber()+1);
//				setSameNum(info.getNumber()+"");
//			}
//		}
//    }
//    public void onCancel(int index){
//    	if (isVoted()) {
//    		totalNum --;
//    		if (getVotedIndex() == index) {
//    			VoteItemInfo info = contents.get(index);
//    			if (info.getNumber() > 0 ) {
//    				info.setNumber(info.getNumber()-1);
//				}
//			}
//		}
//    	if (contents != null) {
//			for(VoteItemInfo info : contents){
//				info.setStatus("0");
//			}
//		}
//    }

	public String getOriginVoteId() {
		return originVoteId;
	}

	public void setOriginVoteId(String originVoteId) {
		this.originVoteId = originVoteId;
	}
	public void updateVote(VoteInfo other){
		if (other != null) {
			setTotalNum(other.totalNum);
			setSameNum(other.sameNum);
			setOriginVoteId(other.getOriginVoteId());
			if (contents != null) {
				for(VoteItemInfo info : contents){
					if (info.getId().equals(other.getVotedId())) {
						info.setStatus("1");
					}else {
						info.setStatus("0");
					}
				}
			}
		}
	}

    public void update(Context context) {
        int total = 0;
        for(VoteItemInfo info : contents) {
            info.setNumber(DataManager.getInstance(context).getOptionCount(info.getId()));
            total += info.getNumber();
        }
        totalNum = total;
    }

    public void update(Context context, OptionResultModel response) {
        int size = 0;
        int itemSize = 0;

        for(VoteItemInfo info : contents) {
            for(OptionItem item : response.data) {
                if(item.optionId.equalsIgnoreCase(info.getId())) {
                    itemSize = item.count;
                    break;
                }
            }
            info.setNumber(itemSize);
            size+=itemSize;

            DataManager.getInstance(context).updateOptionCount(info.getId(), info.getNumber());
        }

        setTotalNum(size);
    }

    public void update(Context context, int selectedIndex, int lastIndex) {
//        if(!isVoted(context))
//            totalNum++;
//        else
//            totalNum--;

        int index = 0;
        int total = 0;
        for(VoteItemInfo info : contents) {
            if(index == lastIndex) {
                info.updateNum(false);
            } else if(index == selectedIndex) {
                info.updateNum(true);
            }
            total += info.getNumber();
            DataManager.getInstance(context).updateOptionCount(info.getId(), info.getNumber());
            index++;
        }
        totalNum = total;
    }

    public void setup(Context context, TimeLinePiece item) {
        setTitle(item.title);
        setType(getType(item.type));
        setContents(getInfos(context, item));
        rid = item.rid;
    }

    private List<VoteItemInfo> getInfos(Context context, TimeLinePiece item) {
        List<VoteItemInfo> lists = new ArrayList<>();
        List<OptionItem> options = item.getOptions();
        VoteItemInfo info;
        int size=0;
        int total=0;
        for(OptionItem option : options) {
            info = new VoteItemInfo();
            info.setSubject(option.optionValue);
            info.setId(option.optionId);
            size = DataManager.getInstance(context).getOptionCount(option.optionId);
//            size = 2;
            total += size;
            info.setNumber(size);
            lists.add(info);
        }
        totalNum = total;
        return lists;
    }

    private VoteInfo.VoteType getType(String type) {
        if(TimeLinePiece.TYPE_VS.equalsIgnoreCase(type))
            return VoteInfo.VoteType.SUPPORT;
        return VoteInfo.VoteType.COMMON;
    }

}
